package com.yuyou.fn.educational.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.Threads;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.common.util.Utils;
import com.yuyou.fn.educational.entity.*;
import com.yuyou.fn.educational.mapper.ContinueClassStatMapper;
import com.yuyou.fn.educational.service.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 *
 * ContinueClassStat 表数据服务层接口实现类
 *
 */
@Service
public class ContinueClassStatServiceImpl extends SuperServiceImpl<ContinueClassStatMapper, ContinueClassStat,Long> implements IContinueClassStatService {

    @Resource
    private IPeriodService periodService ;

    @Resource
    private ICourseClassService courseClassService ;

    @Resource
    private IStudentRegService studentRegService ;

    @Resource
    private ITeacherContinueClassStatService teacherContinueClassStatService ;

    @Resource
    private IClassLectureService classLectureService;


    @Override
    public void calContinueClassRate(List<StudentReg> studentRegList) {



        for(StudentReg studentReg : studentRegList ){

            Period prePeriod = periodService.findPrePeriod(studentReg.getPeriodId());
            if(prePeriod == null ){
                continue;
            }


            //
            List<StudentReg> regList = studentRegService.findStudentRegByPeriodIdStudentId(prePeriod.getPeriodId(), studentReg.getStudentId());

            if(regList.isEmpty() ){
                continue;
            }

            List<Long> classIds = BeanUtils.toList(regList,"classId");

            //重新计算这些班的续读
            for(Long classId : classIds ){

                updateContinueClassStat(classId );
            }

        }


    }

    public  void updateContinueClassStat(Long classId) {

        final CourseClass courseClass = courseClassService.findById(classId);
        List<StudentReg> studentRegList = studentRegService.findStudentRegByClassId(classId);
        List<Long> studentIds = BeanUtils.toList(studentRegList, "studentId");
        //查找下一期
        Period nextPeriod = periodService.findNextPeriod(courseClass.getPeriodId());

        if(nextPeriod == null ){
            return;
        }

        List<StudentReg> studentRegList2 = studentRegService.findStudentRegByAreaTeamIdPeriodIdStudentIds(courseClass.getAreaTeamId(),nextPeriod.getPeriodId(), studentIds);
        List<Long> studentIds2 = new ArrayList<>();//续读学生

        List<Long> classIds2  = BeanUtils.toList(studentRegList2,"classId");
        List<CourseClass> courseClassList = courseClassService.findByIds(classIds2);
        Map<Long, CourseClass> courseClassMap2 = BeanUtils.toMap(courseClassList, "classId");

        for(StudentReg reg : studentRegList2 ){
            CourseClass courseClass2 = courseClassMap2.get(reg.getClassId());
            if(courseClass2 != null && courseClass2.getSubjectId().equals(courseClass.getSubjectId())){//同科目
                studentIds2.add(reg.getStudentId());
            }
        }

        ContinueClassStat continueClassStat = new ContinueClassStat();
        continueClassStat.setUpdateTime(TimeUtils.now());
        continueClassStat.setContinueClassStudentIds(StringUtils.join(studentIds2,","));
        continueClassStat.setContinueClassAmount(studentIds2.size());
        continueClassStat.setTotalAmount(courseClass.getRegAmount());
        continueClassStat.setStudentIds(StringUtils.join(studentIds,","));

        Example<ContinueClassStat> example = ContinueClassStat.createExample();
        example.createCriteria().andEqualTo("classId" , classId );

        boolean b = updateSelectiveByExample(continueClassStat, example);

        if(!b){

            continueClassStat.setAreaTeamId(courseClass.getAreaTeamId());
            continueClassStat.setBusTeamId(courseClass.getBusTeamId());
            continueClassStat.setClassId(classId);
            continueClassStat.setPeriodId(courseClass.getPeriodId());
            continueClassStat.setCreateTime(TimeUtils.now());
            insertSelective(continueClassStat);

        }

        Threads.delay(new Runnable() {
            @Override
            public void run() {
                List<ClassLecture> classLectureList = classLectureService.findClassLectureByClassId(classId);
                List<Long> teacherIds = BeanUtils.toList(classLectureList,"teacherId");
                teacherContinueClassStatService.calTeacherContinueClassRate( courseClass.getAreaTeamId(),courseClass.getBusTeamId() , courseClass.getPeriodId(), Utils.unique(teacherIds));
            }
        } ,10 );
    }


    @Override
    public List<ContinueClassStat> findContinueClassStatByPeriodClassIds(Long periodId, List<Long> classIds) {

        if(classIds.isEmpty() ){
            return Collections.EMPTY_LIST ;
        }

        Example<ContinueClassStat> example = ContinueClassStat.createExample();
        example.createCriteria().andEqualTo("periodId" , periodId ).andIn("classId" , classIds );
        return findListByExample(example);
    }

    @Override
    public ContinueClassStat findContinueClassStatByClassId(Long classId) {
        Example<ContinueClassStat> example = ContinueClassStat.createExample();
        example.createCriteria().andEqualTo("classId" , classId );
        return findOneByExample(example) ;
    }
}